package com.interview.javabase.algorithm;

/**
 * @author: zsg
 * @description:
 * @date: 2019/8/17 11:15
 * @modified:
 */
public class HandWriteQueue {

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(10);
        arrayQueue.pull(1);
        arrayQueue.pull(2);
        arrayQueue.pull(3);
        arrayQueue.pull(4);
        arrayQueue.pull(5);
        arrayQueue.pull(6);
        arrayQueue.pull(7);
        arrayQueue.pull(8);
        arrayQueue.pull(9);
        arrayQueue.pull(10);
        arrayQueue.pull(11);

        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());

        arrayQueue.pull(11);
        arrayQueue.pull(12);
        arrayQueue.pull(13);
        arrayQueue.pull(14);

//        arrayQueue.showQueue();

    }

}

class ArrayQueue{
    private int maxSize;

    private int front;

    private int rear;

    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1;
        this.rear = -1;
        this.arr = new int[maxSize];
    }

    public boolean isFull(){
        return rear == maxSize -1;
    }

    public boolean isEmpty(){
        return rear == front;
    }

    public int get(){
        if (isEmpty()){
            System.out.println("队列没有数据");
            return -1;
        }
        return arr[++front];
    }

    public boolean pull(int value){
        if (isFull()){
            System.out.println("队列已经满了");
            return false;
        }

        arr[++rear] = value;
        return true;
    }

    public void showQueue(){
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
    public int getMaxSize() {
        return maxSize;
    }

    public ArrayQueue setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        return this;
    }

    public int getFront() {
        return front;
    }

    public ArrayQueue setFront(int front) {
        this.front = front;
        return this;
    }

    public int getRear() {
        return rear;
    }

    public ArrayQueue setRear(int rear) {
        this.rear = rear;
        return this;
    }

    public int[] getArr() {
        return arr;
    }

    public ArrayQueue setArr(int[] arr) {
        this.arr = arr;
        return this;
    }
}